
if (WIN32)

    # Helper functions to copy files required for running microbenchmarks
    ## Unconditional copy files
    function(copy_dll_file src_dll_rel dest_dir target_dependent_dll_files)
        string(REPLACE "/" "_" src_dll_path ${src_dll_rel})
        set(DUMMY_NAME "dummy_${src_dll_path}")
        set(src_dll ${CMAKE_SOURCE_DIR}/../libs/${src_dll_rel})
        get_filename_component(dll_name ${src_dll} NAME)
        if (dest_dir STREQUAL "")
            set(dest_dir_path "")
        else()
            set(dest_dir_path "${dest_dir}/")
        endif()
        set(dest_dll ${CMAKE_SOURCE_DIR}/../bin/$<CONFIG>/${dest_dir_path}${dll_name})
        add_custom_command(OUTPUT ${dest_dll}
            COMMAND ${CMAKE_COMMAND} -E copy_if_different "${src_dll}" "${dest_dll}"
            COMMAND ${CMAKE_COMMAND} -E echo "Copy ${src_dll} to ${dest_dll}"
            DEPENDS ${src_dll}
            )
        list(APPEND target_dependent_dll_files ${dest_dll})
        set(target_dependent_dll_files "${target_dependent_dll_files}" PARENT_SCOPE)
    endfunction()

    ## Conditional copy files - only applicable in target is given Config
    function(copy_dll_file_if_config src_dll_rel dest_dir config_list target_dependent_dll_files)
        string(REPLACE "/" "_" src_dll_path ${src_dll_rel})
        set(DUMMY_NAME "${src_dll_path}_dummy")
        set(src_dll ${CMAKE_SOURCE_DIR}/../libs/${src_dll_rel})
        get_filename_component(dll_name ${src_dll} NAME)
        if (dest_dir STREQUAL "")
            set(dest_dir_path "")
        else()
            set(dest_dir_path "${dest_dir}/")
        endif()
        set(dest_dll ${CMAKE_SOURCE_DIR}/../bin/$<CONFIG>/${dest_dir_path}${dll_name})
        foreach(config_name in ${config_list})
            add_custom_command(OUTPUT $<IF:$<CONFIG:${config_name}>,${dest_dll},${DUMMY_NAME}>
                COMMAND ${CMAKE_COMMAND} -E $<IF:$<CONFIG:${config_name}>,copy_if_different,true> "${src_dll}" ${dest_dll}
                COMMAND ${CMAKE_COMMAND} -E $<IF:$<CONFIG:${config_name}>,echo,true> "Copy ${src_dll} to ${${dest_dll}}"
                COMMAND ${CMAKE_COMMAND} -E $<IF:$<CONFIG:${config_name}>,true,touch> ${DUMMY_NAME}
                DEPENDS $<$<CONFIG:${config_name}>:${src_dll}>
                )
            list(APPEND target_dependent_dll_files $<$<CONFIG:${config_name}>:${dest_dll}>)
        endforeach()
        set(target_dependent_dll_files "${target_dependent_dll_files}" PARENT_SCOPE)
    endfunction()

    function (BuildDLLs)
        set(target_dependent_dll_files "")

        # Handle dlls for all targets
        copy_dll_file("NVAPI/x64/nvapi64.dll" "" "${target_dependent_dll_files}")
        copy_dll_file("GPUZ/x64/GpuZ.dll" "" "${target_dependent_dll_files}")

        # Handle dlls for all DX12 builds
        set(DXConfigs "DxDebug;DxRelease;DxDebugExperimental;DxReleaseExperimental")
        copy_dll_file_if_config("WinPix/x64/WinPixEventRuntime.dll" "" "${DXConfigs}" "${target_dependent_dll_files}")
        copy_dll_file_if_config("GPA-AMD/x64/GPUPerfAPIDX12-x64.dll" "" "${DXConfigs}" "${target_dependent_dll_files}")

        # Handle dlls for stable DX12 builds
        set(DXStableConfigs "DxDebug;DxRelease")
        copy_dll_file_if_config("D3D12/x64/D3D12Core.dll" "D3D12" "${DXStableConfigs}" "${target_dependent_dll_files}")
        copy_dll_file_if_config("D3D12/x64/d3d12SDKLayers.dll" "D3D12" "${DXStableConfigs}" "${target_dependent_dll_files}")
        copy_dll_file_if_config("D3D12/x64/dxcompiler.dll" "" "${DXStableConfigs}" "${target_dependent_dll_files}")
        copy_dll_file_if_config("D3D12/x64/dxil.dll" "" "${DXStableConfigs}" "${target_dependent_dll_files}")

        # Handle dlls for experimental DX12 builds
        set(DXExperimentalConfigs "DxDebugExperimental;DxReleaseExperimental")
        copy_dll_file_if_config("D3D12P/x64/D3D12Core.dll" "D3D12" "${DXExperimentalConfigs}" "${target_dependent_dll_files}")
        copy_dll_file_if_config("D3D12P/x64/d3d12SDKLayers.dll" "D3D12" "${DXExperimentalConfigs}" "${target_dependent_dll_files}")
        copy_dll_file_if_config("D3D12P/x64/dxcompiler.dll" "" "${DXExperimentalConfigs}" "${target_dependent_dll_files}")

        # Handle dlls for all VK builds
        set(VKConfigs "VkDebug;VkRelease")
        copy_dll_file_if_config("GLFW/x64/glfw3.dll" "" "${VKConfigs}" "${target_dependent_dll_files}")

        add_custom_target(DllFiles
                            DEPENDS ${target_dependent_dll_files}
                            )
        set_target_properties(DllFiles PROPERTIES FOLDER "_CoreDependencies")
    endfunction()

endif() # WIN32
